package com.example.myapplication;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LongDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.adapter.NewsListAdapter;
import com.example.myapplication.bean.NewsListBean;
import com.example.myapplication.util.Network;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class MyFragment extends Fragment  implements OnLoadMoreListener,OnRefreshListener{
    private final String TAG = "MyFragment";
    private String category; // 分类标识
    private RecyclerView recyclerView;
    private RefreshLayout mRefreshLayout ;
    private int page = 1;
    private int page_size = 5;
    NewsListAdapter newsListAdapter;


    public static MyFragment newInstance(String category) {
        MyFragment fragment = new MyFragment();
        // 创建一个Bundle来存储传递给Fragment的参数
        Bundle args = new Bundle();
        args.putString("category", category);
        // 将这个Bundle设置为Fragment的arguments

        fragment.setArguments(args);
        return fragment;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment, container, false);
        recyclerView = view.findViewById(R.id.rv_news_list);
        mRefreshLayout = view.findViewById(R.id.refreshLayout);
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setOnLoadMoreListener(this);
        initData();
        return view;
    }

    private void initData() {
      Network.doGet(getContext(),"index?type="+ category +"&page="+page+"&page_size="+page_size+"&is_filter=0&key=", new Network.OkResult() {
                @Override
                public void success(JSONObject jsonObject) {
                    JSONObject resultObject = null;
                    try {
                        resultObject = jsonObject.getJSONObject("result");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    String dataJson = resultObject.optString("data");
                    List<NewsListBean> homeList1Beans = new Gson().fromJson(dataJson, new TypeToken<List<NewsListBean>>(){}.getType());

                    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                     newsListAdapter = new NewsListAdapter(getActivity(), homeList1Beans);
                    recyclerView.setAdapter(newsListAdapter);
                }
                @Override
                public void error(String message) {

                }
            });
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            category = getArguments().getString("category");
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        //加载更多完成
        page_size += 5;
        refreshData(0,page,page_size);
        mRefreshLayout.finishLoadMore(1000);

    }



    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        //刷新加载完成
        page++;
        refreshData(1,page,5);
        mRefreshLayout.finishRefresh(1000);

    }

    //刷新数据
    private void refreshData(int state ,int page, int page_size) {
        Network.doGet(getContext(),"index?type="+ category +"&page="+page+"&page_size="+page_size+"&is_filter=0&key=", new Network.OkResult() {
            @Override
            public void success(JSONObject jsonObject) {
                JSONObject resultObject = null;
                try {
                    resultObject = jsonObject.getJSONObject("result");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String dataJson = resultObject.optString("data");
                List<NewsListBean> homeList1Beans = new Gson().fromJson(dataJson, new TypeToken<List<NewsListBean>>(){}.getType());
                if (state == 1){
                    //刷新加载完成
                    newsListAdapter.setData(homeList1Beans);
                }else{
                    //加载更多完成
                    //找出不同部分
                    int index = Math.min(5, homeList1Beans.size());
                    List<NewsListBean> newsListBeans = homeList1Beans.subList(homeList1Beans.size() - index, homeList1Beans.size());
                    newsListAdapter.appendData(newsListBeans);
                }
            }
            @Override
            public void error(String message) {

            }
        });


    }
}
